package com.societyGames.outputExercises
{
import com.societyGames.action.ActionQueue;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;

public class OutputRender extends Sprite
{
  [Embed(source="../../../../../OutputExercises/assets/Cat.png")]
  private static var CatImageClass:Class;

  [Embed(source="../../../../../OutputExercises/assets/Face.png")]
  private static var FaceImageClass:Class;

  private static const DISPLAY_WIDTH:int = 250; //TODO: Move this to shared location.
  private static const DISPLAY_HEIGHT:int = 250;
  private static const LEFT_GUTTER_SIZE:int = 5;
  private static const RIGHT_GUTTER_SIZE:int = 5;
  private static const TOP_GUTTER_SIZE:int = 5;
  private static const BOTTOM_GUTTER_SIZE:int = 34;

  private var _outputSimulation:OutputSimulation;
  private var _actionQueue:ActionQueue;
  private var _desiredText:String = "";
  private var _desiredTextField:TextField;
  private var _outputText:String = "";
  private var _outputTextField:TextField;
  private var _errorTextFormat:TextFormat;
  private var _firstErrorIndex:int = -1;

  public function OutputRender(outputSimulation:OutputSimulation, actionQueue:ActionQueue)
  {
    this._outputSimulation = outputSimulation;
    this._actionQueue = actionQueue;

    var desiredBackground:DisplayObject = DisplayObject(new FaceImageClass());
    addChild(desiredBackground);

    var outputBackground:DisplayObject = DisplayObject(new CatImageClass());
    addChild(outputBackground);
    outputBackground.x = desiredBackground.width;

    this._desiredTextField = createTextField();
    this._desiredTextField.x = LEFT_GUTTER_SIZE;
    this._desiredTextField.y = TOP_GUTTER_SIZE;

    this._outputTextField = createTextField();
    this._outputTextField.x = (DISPLAY_WIDTH / 2) + LEFT_GUTTER_SIZE;
    this._outputTextField.y = TOP_GUTTER_SIZE;

    _errorTextFormat = this._outputTextField.defaultTextFormat;
    _errorTextFormat.color = 0xFF0000;
    _errorTextFormat.align = TextFormatAlign.CENTER;
  }

  private function createTextField():TextField
  {
    var textField:TextField = new TextField();
    textField.width = (DISPLAY_WIDTH / 2) - LEFT_GUTTER_SIZE - RIGHT_GUTTER_SIZE;
    textField.height = DISPLAY_HEIGHT - TOP_GUTTER_SIZE - BOTTOM_GUTTER_SIZE;

    var textFormat:TextFormat = textField.getTextFormat();
    textFormat.align = TextFormatAlign.CENTER;
    textField.defaultTextFormat = textFormat;

    this.addChild(textField);
    return textField;
  }

  public function setDesiredText(text:String):void
  {
    _desiredText = text;
    _desiredTextField.text = text;
  }

  public function addOutputText(text:String):void
  {
    if (_outputText.length > 0)
    {
      text = "\n" + text;
    }

    var newStartIndex:int = _outputText.length;
    _outputText += text;

    _outputTextField.appendText(text);
    for (var index:int = newStartIndex; index < _outputText.length; index++)
    {
      if (index >= _desiredText.length || _outputText.charCodeAt(index) != _desiredText.charCodeAt(index))
      {
        if (_firstErrorIndex == -1)
        {
          _firstErrorIndex = index;
        }
        _outputTextField.setTextFormat(_errorTextFormat, index, index + 1);
      }
    }
  }

  public function showFirstError():void
  {
    if (_firstErrorIndex == -1)
    {
      scrollToEndOfTextField(_desiredTextField);
      scrollToEndOfTextField(_outputTextField);
    }
    else
    {
      scrollToError(_desiredTextField, _firstErrorIndex);
      scrollToError(_outputTextField, _firstErrorIndex);
    }
  }

  private function scrollToEndOfTextField(textField:TextField):void
  {
    textField.scrollV = textField.maxScrollV;
    textField.scrollH = textField.maxScrollH;
  }

  private function scrollToError(textField:TextField, index:int):void
  {

  }
}
}